package Model.Wall;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;

import DBAccess.DBAccess;

public class Post {

	private int userID;
	private int fromID;
	private int postTime;
	private String message = "";

	public Post(int userID, int fromID, int postTime, String message) {
		this.userID=userID;
		this.fromID=fromID;
		this.postTime=postTime;
		this.message=message;
		if(message!=null){
			//System.out.println("Good");
		}
		try {
			Statement st = (Statement) DBAccess.con.createStatement();
			st.executeUpdate("INSERT INTO walldb (userID, fromID, deleteFlag, message, parentID, postTime) "
					+ "VALUES ( "
					+ userID
					+ ","
					+ fromID
					+ ","
					+ "false,'"
					+ message + "', -1, " + postTime + " )");
		} catch (Exception e) {
			System.err.println("Got an exception! ");
			System.err.println(e.getMessage());
		}
	}
	
	public int getPostID(){
		String cmd = "SELECT postID FROM walldb WHERE userID =" +userID+ " AND fromID="+fromID+" AND postTime=" +postTime+ 
				" AND message='"+message+"'";
		
		PreparedStatement ps = null;
		ResultSet rs = null;
		int postID=-1;
		try {
			ps = DBAccess.con.prepareStatement(cmd);
			rs = ps.executeQuery();
			if (rs.next()) {
				postID = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DBAccess.close(ps, rs);
		return postID;
	}

	
	public int getUserID(){
		return userID;
	}

	public int getFromID() {
		return fromID;
	}
	public static int getFromIDUsingPostID(int postID){
		String cmd = "SELECT fromID FROM walldb WHERE postID =" +postID;
		
		PreparedStatement ps = null;
		ResultSet rs = null;
		int fromID=-1;
		try {
			ps = DBAccess.con.prepareStatement(cmd);
			rs = ps.executeQuery();
			if (rs.next()) {
				fromID = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DBAccess.close(ps, rs);
		return fromID;
	}

	
	public int getPostTime(){
		return postTime;
	}
	//not sure why we need this
//	public int getToID(int userID) {
//		String cmd = "SELECT fromID, FROM walldb where userid =?";
//		
//		PreparedStatement ps = null;
//		ResultSet rs = null;
//		int toID=-1;
//		try {
//			ps = DBAccess.con.prepareStatement(cmd);
//			ps.setInt(1, userID);
//			rs = ps.executeQuery();
//			if (rs.next()) {
//				toID = rs.getInt("fromID");
//			}
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		DBAccess.close(ps, rs);
//		return toID;
//	}

	public Boolean getDeleteFlag() {
		return false;
	}

	public String getMessage() {
		return message;
	}
	public static String getMessageFromPostID(int postID) {
		String cmd = "SELECT name FROM accountdb A, walldb W where A.userID = W.userID and postID = " +postID;
		
		PreparedStatement ps = null;
		ResultSet rs = null;
		String message="";
		try {
			ps = DBAccess.con.prepareStatement(cmd);
			rs = ps.executeQuery();
			if (rs.next()) {
				message = rs.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DBAccess.close(ps, rs);
		return message;
	}

}